package com.daxidi.cn.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

/**
 * @author jialin
 * @createTime 2020-09-03
 */
public class UuidUtils {

    //sh1
    /**
     * 将字符串进行sha1加密
     *
     * @param str 需要加密的字符串
     * @return 加密后的内容
     */
    public static String sha1(String str) {
        try {
            MessageDigest digest = MessageDigest.getInstance("SHA-1");
            digest.update(str.getBytes());
            byte messageDigest[] = digest.digest();
            // Create Hex String
            StringBuffer hexString = new StringBuffer();
            // 字节数组转换为 十六进制 数
            for (int i = 0; i < messageDigest.length; i++) {
                String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
                if (shaHex.length() < 2) {
                    hexString.append(0);
                }
                hexString.append(shaHex);
            }
            return hexString.toString();

        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    //sessionId
    /*String sessionId = session.getId();
    Long timestamp = System.currentTimeMillis();
    sysUserUuid.setId(timestamp + sessionId.substring(0,12));*/

    //生成id，时间戳+12位UUID
    //排序：order by mid(id , 1 , 13)  -> 取出前13个字符，根据时间排序
    //Long timestamp = System.currentTimeMillis();
    //String uuid = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 12);


    //获取16位UUID
    public static String getUUID(){
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0,8);
    }

    //获取13位时间戳+12位UUID
    public static String getTimeUUID(){
        return System.currentTimeMillis() + UUID.randomUUID().toString().replaceAll("-", "").substring(0,12);
    }

    //创建订单号
    public static String createOID(String gId){
        String oIdPrefix = UUID.randomUUID().toString().replaceAll("-", "").substring(0,5);
        return oIdPrefix + System.currentTimeMillis() + gId;
    }

    public static String createNonceStr(){
        return UUID.randomUUID().toString().replaceAll("-","").substring(0,32);
    }

    public static void main(String[] args) {
        String s = sha1("123");
        System.out.println(s);
    }
}
